const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
    username: { type: String },
    password: { type: String },
    roles: { type: String }
})

const User = mongoose.model("User", userSchema, "users")

module.exports = {
    init() {
        mongoose
            .connect("mongodb://127.0.0.1:27017/2302B", {
                useNewUrlParser: true,
            })
            .then(() => {
                console.log('成功连接到MongoDB数据库2302B')
            })
            .catch((e) => {
                console.log('链接失败')
                console.warn(e)
            })
    },
    async checkUser(username, password) {
        const user = await User.findOne({ username })
        return user && user.password == password
    },
    async register(username, password) {
        const user = await User.insertMany([{
            username: username,
            password: password
        }])
        return user && '注册成功'
    }
}